package com.timguan.render.dao.manager.cms;

import com.timguan.render.dao.domain.cms.CmsDataBindingDO;
import com.timguan.render.dao.domain.cms.CmsDataBindingDOExample;
import com.timguan.render.dao.persistence.cms.CmsDataBindingDOMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by guankaiqiang on 2015/3/20.
 */
public class CmsDataBindingManagerImpl {
    @Resource
    private CmsDataBindingDOMapper cmsDataBindingDOMapper;

    public int insert(CmsDataBindingDO record) {
        int id = 0;
        if (record == null) {
            return id;
        }
        Date now = new Date();
        record.setGmtCreate(now);
        record.setGmtModified(now);
        record.setIsDelete(0);
        cmsDataBindingDOMapper.insert(record);
        return record != null ? record.getId() : 0;
    }

    public int updateByPrimaryKeySelective(CmsDataBindingDO record) {
        int count = 0;
        if (record == null) {
            return count;
        }
        Date now = new Date();
        record.setGmtModified(now);
        count = cmsDataBindingDOMapper.updateByPrimaryKeySelective(record);
        return count;
    }

    public int updateByExampleSelective(CmsDataBindingDO record, CmsDataBindingDOExample example) {
        int count = 0;
        if (record == null) {
            return count;
        }
        Date now = new Date();
        record.setGmtModified(now);
        count = cmsDataBindingDOMapper.updateByExampleSelective(record, example);
        return count;
    }

    public int insertOrUpdate(CmsDataBindingDO record) {
        if (record == null) {
            return 0;
        }
        Date now = new Date();
        CmsDataBindingDOExample condition = new CmsDataBindingDOExample();
        condition.createCriteria().andIsDeleteEqualTo(0).andPageIdEqualTo(record.getPageId()).andPlaceholderEqualTo(
                record.getPlaceholder()).andSubmoduleIdEqualTo(record.getSubmoduleId());
        List<CmsDataBindingDO> cmsDataBindingDOs = cmsDataBindingDOMapper.selectByExample(condition);
        if (cmsDataBindingDOs != null && cmsDataBindingDOs.size() > 0) {
            record.setGmtModified(now);
            cmsDataBindingDOMapper.updateByExampleSelective(record, condition);
            return cmsDataBindingDOs.get(0).getId();
        } else {
            record.setGmtCreate(now);
            record.setGmtModified(now);
            record.setIsDelete(0);
            cmsDataBindingDOMapper.insert(record);
            return record != null ? record.getId() : 0;
        }
    }

    public List<CmsDataBindingDO> getBindingListIdsOfPage(int pageId) {
        CmsDataBindingDOExample condition = new CmsDataBindingDOExample();
        condition.createCriteria().andPageIdEqualTo(pageId).andIsDeleteEqualTo(0);
        condition.setOrderByClause("submodule_id asc");
        return cmsDataBindingDOMapper.selectByExample(condition);
    }

    public CmsDataBindingDO getBindingInfoByPageIdAndListId(int pageId, int listId) {
        CmsDataBindingDOExample condition = new CmsDataBindingDOExample();
        condition.createCriteria().andPageIdEqualTo(pageId).andListIdEqualTo(listId).andIsDeleteEqualTo(0);
        List<CmsDataBindingDO> cmsDataBindingDOs = cmsDataBindingDOMapper.selectByExample(condition);
        if (cmsDataBindingDOs != null && cmsDataBindingDOs.size() != 0) {
            return cmsDataBindingDOs.get(0);
        }
        return null;
    }

    //    @Transactional("transactionManager")
    //    public int insertDatalistAndBind(String processor, Integer pageId, String placeHolder, Integer submoduleId) {
    //        CmsDatalistDO cmsDatalistDO = new CmsDatalistDO();
    //        //        cmsDatalistDO.setType(type);
    //        //        cmsDatalistDO.setShowFields(showFields);
    //        //        cmsDatalistDO.setCount(count);
    //        Date date = new Date();
    //        cmsDatalistDO.setGmtCreate(date);
    //        cmsDatalistDO.setGmtModified(date);
    //        cmsDatalistDO.setIsDelete(0);
    //        cmsDataListDOMapper.insert(cmsDatalistDO);
    //        if (cmsDatalistDO != null) {
    //            int dataListId = cmsDatalistDO.getId();
    //            CmsDataBindingDO cmsDataBindingDO = new CmsDataBindingDO();
    //            Map<String, Object> map = new HashMap<String, Object>();
    //            map.put("listId", dataListId);
    //            cmsDataBindingDO.setProcessorParams(JSON.toJSonString(listId));
    //            cmsDataBindingDO.setPageId(pageId);
    //            cmsDataBindingDO.setPlaceholder(placeHolder);
    //            cmsDataBindingDO.setSubmoduleId(submoduleId);
    //            cmsDataBindingDO.setProcessor(processor);
    //            return insertOrUpdate(cmsDataBindingDO);
    //        } else {
    //            return 0;
    //        }
    //    }
}
